document.addEventListener('DOMContentLoaded', function() {
    const input = document.querySelector('.input-container input');
    const sendButton = document.querySelector('.send-button');
    const chatMain = document.querySelector('.chat-main');
    
    // 快捷键监听
    document.addEventListener('keydown', function(e) {
        if (e.ctrlKey && e.key === 'u') {
            e.preventDefault();
            input.focus();
        }
    });

    // 命令提示功能
    input.addEventListener('input', function(e) {
        if (e.target.value === '/') {
            // 这里可以显示命令提示列表
        }
    });

    function renderCodeBlock(code) {
        // 处理代码高亮
        const highlightedCode = code
            .replace(/(public|class|static|void)/g, '<span class="keyword">$1</span>')
            .replace(/(String|System)/g, '<span class="type">$1</span>')
            .replace(/"([^"]*)"/g, '<span class="string">"$1"</span>');

        return `
            <div class="code-language">java</div>
            <div class="code-content">${highlightedCode}</div>
        `;
    }

    function renderExplanation(explanations) {
        return `
            <div class="code-explanation">
                <div class="explanation-section">
                    <div class="explanation-title">### 解释：</div>
                    ${explanations.map(exp => `
                        <div class="explanation-item">
                            <div class="explanation-item-title">${exp.title}</div>
                            <div class="explanation-item-content">${exp.content}</div>
                        </div>
                    `).join('')}
                </div>
            </div>
        `;
    }

    function renderCommandBlock(commands) {
        return `
            <div class="command-block">
                ${commands.map(cmd => `
                    <div class="command-line">${cmd}</div>
                `).join('')}
            </div>
        `;
    }

    function renderMessage(message) {
        if (message.type === 'code') {
            return `
                <div class="code-block">
                    ${renderCodeBlock(message.code)}
                    ${renderExplanation(message.explanations)}
                    ${message.commands ? renderCommandBlock(message.commands) : ''}
                </div>
            `;
        }
        return `<div class="message">${message}</div>`;
    }

    // 修改发送消息函数
    function sendMessage() {
        const message = input.value.trim();
        if (message) {
            // 示例：处理代码消息
            if (message.includes('生成一个Java Hello World')) {
                const codeMessage = {
                    type: 'code',
                    code: `public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}`,
                    explanations: [
                        {
                            title: '1. public class HelloWorld：',
                            content: '定义了一个名为 HelloWorld 的公共类。在Java中，文件名必须与公共类名相同。'
                        },
                        {
                            title: '2. public static void main(String[] args)：',
                            content: '这是Java程序的入口点。main 方法是Java程序的起始点。'
                        },
                        {
                            title: '3. System.out.println("Hello, World!")：',
                            content: '这行代码用于在控制台输出 "Hello, World!"。System.out 是标准输出流。'
                        }
                    ],
                    commands: [
                        'javac HelloWorld.java',
                        'java HelloWorld'
                    ]
                };
                const messageElement = document.createElement('div');
                messageElement.innerHTML = renderMessage(codeMessage);
                chatMain.appendChild(messageElement);
            } else {
                // 处理普通消息
                const messageElement = document.createElement('div');
                messageElement.className = 'message';
                messageElement.textContent = message;
                chatMain.appendChild(messageElement);
            }
            
            input.value = '';
            chatMain.scrollTop = chatMain.scrollHeight;
        }
    }

    sendButton.addEventListener('click', sendMessage);
    input.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            sendMessage();
        }
    });

    // 快捷功能卡片点击
    document.querySelectorAll('.action-card').forEach(card => {
        card.addEventListener('click', function() {
            const action = this.querySelector('p').textContent;
            input.value = action;
            input.focus();
        });
    });
}); 